﻿''' <summary>
''' Represents a single user in a withingsaccount
''' </summary>
''' <remarks></remarks>
Public Class User

    Private m_userId As Integer
    Private m_publicKey As String

    Private m_firstName As String
    Private m_lastName As String
    Private m_shortName As String
    Private m_gender As GenderEnum
    Private m_fatMethod As String
    Private m_birthdate As DateTime
    Private m_isBodyScaleDataPublic As Boolean
    Private m_isBloodPressureMonitorDataPublic As Boolean


    ''' <summary>
    ''' Gets the given userID (constructor parameter)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property UserId() As Integer
        Get
            Return m_userId
        End Get
    End Property
    ''' <summary>
    ''' Gets the Withings-publicKey, given in the constructor
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property PublicKey() As String
        Get
            Return m_publicKey
        End Get
    End Property
    ''' <summary>
    ''' The user's firstname, as an UTF-8 encoded string
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property FirstName() As String
        Get
            Return m_firstName
        End Get
    End Property
    ''' <summary>
    ''' The user's lastname, as an UTF-8 encoded string
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property LastName() As String
        Get
            Return m_lastName
        End Get
    End Property
    ''' <summary>
    ''' The user's shortname
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ShortName() As String
        Get
            Return m_shortName
        End Get
    End Property
    ''' <summary>
    ''' The user's gender
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Gender() As GenderEnum
        Get
            Return m_gender
        End Get
    End Property
    ''' <summary>
    ''' Byte indicating the Body Composition Formula in use
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property FatMethod() As String
        Get
            Return m_fatMethod
        End Get
    End Property
    ''' <summary>
    ''' The user's birthdate
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Birthdate() As DateTime
        Get
            Return m_birthdate
        End Get
    End Property
    ''' <summary>
    ''' User shares bodyscaledate
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property IsBodyScaleDataPublic() As Boolean
        Get
            Return m_isBodyScaleDataPublic
        End Get
    End Property
    ''' <summary>
    ''' user shares bloodpreasuredata
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property IsBloodPressureMonitorDataPublic() As Boolean
        Get
            Return m_isBloodPressureMonitorDataPublic
        End Get
    End Property


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="startDate">Will prevent retrieval of values dated prior to the supplied parameter.</param>
    ''' <param name="endDate">Will prevent retrieval of values dated after the supplied parameter.</param>
    ''' <param name="devType">Will retrieve data for this particular device type.</param>
    ''' <param name="measType">Will restrict the type of data retrieved (see below the type list). Please note that currently this filter only supports weight (ie type 1) height (ie type 4) types.</param>
    ''' <param name="lastUpdate">Is useful for performing data synchronization.</param>
    ''' <param name="category">Can be set to IsTarget to retrieve objectives or to IsMeasure to retrieve actual measurements.</param>
    ''' <param name="limit">Can be used to limit the number of measure groups returned in the result.</param>
    ''' <param name="offset">Can be used to skip the 'offset' most recent measure group records of the result set.</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ReadMeasures(ByVal startDate As DateTime?, _
                                 ByVal endDate As DateTime?, _
                                 ByVal lastUpdate As DateTime?, _
                                 Optional ByVal devType As DeviceTypeEnum = DeviceTypeEnum.All, _
                                 Optional ByVal measType As MeasureTypeEnum = MeasureTypeEnum.None, _
                                 Optional ByVal category As CategoryEnum = CategoryEnum.None, _
                                 Optional ByVal limit As Integer = -1, _
                                 Optional ByVal offset As Integer = -1) As GetMeasuresResponse


        Dim retVal As GetMeasuresResponse
        Dim connector As Core.WBSAPIConnector


        Dim gmstartDate As Integer = -1
        Dim gmendDate As Integer = -1
        Dim referenceDate As DateTime = Nothing
        Dim gmdevType As Integer = -1
        Dim gmMesaType As Integer = -1
        Dim gmLastUpdate As Integer = -1
        Dim gmCategory As Integer = -1

        If startDate.HasValue Then gmstartDate = Core.Helper.DateTimeToEPOCH(startDate.Value)
        If endDate.HasValue Then gmendDate = Core.Helper.DateTimeToEPOCH(endDate.Value)
        If lastUpdate.HasValue Then gmLastUpdate = Core.Helper.DateTimeToEPOCH(lastUpdate.Value)
        If devType <> DeviceTypeEnum.All Then gmdevType = CInt(devType)
        If measType <> MeasureTypeEnum.None Then gmMesaType = CInt(measType)
        If category <> CategoryEnum.None Then gmCategory = CInt(category)

        connector = New Core.WBSAPIConnector
        retVal = connector.GetMeasures(m_userId, m_publicKey, gmstartDate, gmendDate, gmdevType, _
                                       gmMesaType, gmLastUpdate, gmCategory, limit, offset)
        connector = Nothing

        Return retVal
    End Function

    ''' <summary>
    ''' Sets the datas public state - important: you have to reload the user aufter updateing!!
    ''' </summary>
    ''' <param name="setBodyScaleDatePublic">true, if you want to set the body Scale data on public</param>
    ''' <param name="setBloodPressureMonitorDataPublic">true, if you want to set the blood pressure monitor data on public</param>
    ''' <returns>ture, if operation was successful</returns>
    ''' <remarks></remarks>
    Public Function SetPublic(ByVal setBodyScaleDatePublic As Boolean, ByVal setBloodPressureMonitorDataPublic As Boolean) As UpdateUserResponse
        Dim isPublic As Integer = 0
        Dim retVal As UpdateUserResponse
        Dim connector As Core.WBSAPIConnector

        If setBodyScaleDatePublic Then isPublic += 1
        If setBloodPressureMonitorDataPublic Then isPublic += 4

        connector = New Core.WBSAPIConnector
        retVal = connector.UpdateUser(m_userId, m_publicKey, isPublic)
        connector = Nothing

        Return retVal
    End Function

    ''' <summary>
    ''' Loads user by userid and public key
    ''' </summary>
    ''' <param name="userID"></param>
    ''' <param name="publicKey"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetUserByUserId(ByVal userId As Integer, ByVal publicKey As String) As GetUserByIDResponse

        If userId <= 0 Then _
          Throw New ArgumentNullException("userId")

        If String.IsNullOrEmpty(publicKey) Then _
            Throw New ArgumentNullException("publicKey")


        Dim retVal As GetUserByIDResponse
        Dim connector As Core.WBSAPIConnector


        connector = New Core.WBSAPIConnector
        retVal = connector.GetUser(userId, publicKey)
        connector = Nothing

        Return retVal
    End Function

    ''' <summary>
    ''' Creates a new Withings Body Metrics Service Connector.
    ''' </summary>
    ''' <param name="userID">Users WithingsID</param>
    ''' <param name="publicKey">Public Key for users account</param>
    ''' <exception cref="System.ArgumentException">throws an exception, if userID is less or equal zero</exception>
    ''' <exception cref="System.ArgumentNullException">if parameter publicKey is Null or empty</exception>
    ''' <remarks></remarks>
    Public Sub New(ByVal userId As Integer, ByVal publicKey As String)
        If CStr(userId).Length <= 0 Then _
            Throw New ArgumentNullException("userId")

        If String.IsNullOrEmpty(publicKey) Then _
            Throw New ArgumentNullException("publicKey")

        m_userId = userId
        m_publicKey = publicKey
    End Sub
    ''' <summary>
    ''' This construcutr should be by the Core.WBSAPIConnector only
    ''' </summary>
    ''' <param name="userID"></param>
    ''' <param name="publicKey"></param>
    ''' <param name="firstName"></param>
    ''' <param name="lastName"></param>
    ''' <param name="shortName"></param>
    ''' <param name="gender"></param>
    ''' <param name="fatMethod"></param>
    ''' <param name="birthdate"></param>
    ''' <param name="isBodyScaleDataPublic"></param>
    ''' <param name="isBloodPressureMonitorDataPublic"></param>
    ''' <remarks></remarks>
    Friend Sub New(ByVal userID As Integer, _
                   ByVal publicKey As String, _
                   ByVal firstName As String, _
                   ByVal lastName As String, _
                   ByVal shortName As String, _
                   ByVal gender As GenderEnum, _
                   ByVal fatMethod As String, _
                   ByVal birthdate As DateTime, _
                   ByVal isBodyScaleDataPublic As Boolean, _
                   ByVal isBloodPressureMonitorDataPublic As Boolean)


        m_userId = userID
        m_publicKey = publicKey
        m_firstName = firstName
        m_lastName = lastName
        m_shortName = shortName
        m_gender = gender
        m_fatMethod = fatMethod
        m_birthdate = birthdate
        m_isBodyScaleDataPublic = isBodyScaleDataPublic
        m_isBloodPressureMonitorDataPublic = isBloodPressureMonitorDataPublic

    End Sub



End Class

